
var app = getApp();
var appData = app.globalData;
Page({
  data: {
    canGetCode: true,
    phoneNumber: "",
    time:60,
    timer:null,
    check:true,
    onoff:true
  },
  toAgree: function () {
    console.log("....");
    this.setData({
      isAgree: !this.data.isAgree
    });
  },
  getPhone: function (e) {
    console.log(e);
    this.setData({
      phoneNumber: e.detail.value
    })
  },
  codeDone: function (e) {

    if(e.detail.value){
      this.setData({
        code: e.detail.value,
        check:false
      })
    }
  },
  getCode: function () {
    var self = this;
    if(!self.data.onoff){return false;}
    if(!/^1[0-9]{10}$/.test(this.data.phoneNumber)){
      wx.showToast({title:'请输入正确的手机号',icon:'none',duration:2000})
      return false;
    }
    self.data.onoff=false;
    appData.Tool.checkLoginNameV1({ loginName: this.data.phoneNumber }).then(function (res) {
      wx.hideLoading();
      console.log(res);
      if (res.code == 0) {
        wx.showToast({
          title: '已发送验证码',
          duration:2000
        });
        self.setData({
          canGetCode: false,
        });
        self.timer = setInterval(function(){
          self.setData({
            time: --self.data.time <= 0 ? 0 : self.data.time
          });
          self.setData({
            isAgree: self.data.time < -0 ? true : false,
            canGetCode: self.data.time < -0 ? true : false
          });
          if (self.data.time <= 0){
            clearInterval(self.timer)
            self.setData({
              canGetCode: true,
              time:60,
              onoff:true
            })
          }
        },1000);
      } else {
        self.setData({
          canGetCode: false,
          onoff:true
        });
        wx.showToast({
          title: res.message,
          icon: 'none',
          duration:2000
        });
      }
    }).catch(function (error) {
      self.setData({
        canGetCode: false,
        onoff:true
      });
      wx.hideLoading();
      wx.showToast({
        title: error.message,
        icon: 'none',
        duration: 2000
      })
    });
  },
  register: function () {
    var self = this;
    if(!/^1[0-9]{10}$/.test(this.data.phoneNumber)){
      wx.showToast({title:'请输入正确的手机号',icon:'none',duration:2000})
      return false;
    }
    if(!self.data.code){
      wx.showToast({title:'请输入验证码',icon:'none',duration:2000})
      return false;
    }
    appData.Tool.register({ loginName: self.data.phoneNumber, smsCode: self.data.code+"", registerFlag: "sms" }).then(function (res) {
      wx.hideLoading();
      console.log(res);
      if (res.code == 0) {
        console.warn(res);
        wx.token=res.data.token;
        res.data.token && wx.setStorageSync('token', res.data.token);
        app.loginBack();
      } else {
        wx.showToast({
          title: res.message,
        })
        return
      }
    }).catch(function (error) {
      console.log(error);
      wx.hideLoading();
      wx.showToast({
        title: error.message,
        icon: 'none',
        duration: 2000
      })
    });
  },
  onUnload: function () {
    clearInterval(this.timer);
    this.setData({
      canGetCode: true,
      phoneNumber: "",
      code: "",
      isAgree: false,
      time:60,
      timer:null
    })
  }
})

